from tools.listNode import ListNode, listToNode, nodeToList


def mergeTwoLists(l1, l2):
    res = ListNode()
    cur = res
    while l1 and l2:
        if l1.val < l2.val:
            cur.next = l1
            l1 = l1.next
        elif l1.val > l2.val:
            cur.next = l2
            l2 = l2.next
        else:
            cur.next = l1
            l1 = l1.next
            cur = cur.next
            cur.next = l2
            l2 = l2.next
        cur = cur.next
    while l1 and not l2:
        cur.next = l1
        break
    while l2 and not l1:
        cur.next = l2
        break
    return res.next


l1 = listToNode([1, 2, 4])
l2 = listToNode([1, 3, 4])
print(nodeToList(mergeTwoLists(l1, l2)))  # [1,1,2,3,4,4]
